package com.zkthink.ceres.product.service.impl;

import com.google.common.collect.Sets;
import com.zkthink.base.R;
import com.zkthink.cache.repository.RedisRepositoryImpl;
import com.zkthink.ceres.promotion.service.PromotionService;
import com.zkthink.ceres.promotion.vo.web.PromotionShowVO;
import com.zkthink.ceres.promotion.vo.web.PromotionVO;
import com.zkthink.database.mybatis.conditions.Wraps;
import com.zkthink.dozer.DozerUtils;
import com.zkthink.ceres.common.service.SerialService;
import com.zkthink.ceres.member.entity.MemberAddress;
import com.zkthink.ceres.member.service.MemberAddressService;
import com.zkthink.ceres.product.constants.ProductConstant;
import com.zkthink.ceres.product.dao.*;
import com.zkthink.ceres.product.dto.*;
import com.zkthink.ceres.product.entity.*;
import com.zkthink.ceres.product.service.LogisticsService;
import com.zkthink.ceres.product.service.ProductService;
import com.zkthink.base.service.SuperServiceImpl;

import com.zkthink.ceres.product.vo.*;
import com.zkthink.ceres.store.entity.Store;
import com.zkthink.ceres.store.service.StoreService;
import jline.console.history.History;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * <p>
 * 业务实现类
 * 商品表
 * </p>
 *
 * @author JustArgo
 * @date 2020-05-03
 */
@Slf4j
@Service
@AllArgsConstructor
public class ProductServiceImpl extends SuperServiceImpl<ProductMapper, Product> implements ProductService {

    private ProductAttrMapper productAttrMapper;

    private SerialService serialService;

    private RedisRepositoryImpl redisRepository;

    private ProductAttrValueMapper productAttrValueMapper;

    private ProductImgMapper productImgMapper;

    private  ProductDiscountConfigMapper productDiscountConfigMapper;

    private ProductContributionConfigMapper productContributionConfigMapper;

    private ProductDiscContriConfigMapper productDiscContriConfigMapper;

    private ProductSkuMapper productSkuMapper;

    private ProductSkuAttrMapper productSkuAttrMapper;

    private ProductAttrValueMapper proudctAttrValueMapper;

    private DozerUtils dozerUtils;

    private StoreService storeService;

    private MemberAddressService memberAddressService;

    private LogisticsService logisticsService;

    private ProductGroupMapper productGroupMapper;

    private ProductCategoryMapper productCategoryMapper;

    private SearchHistoryMapper searchHistoryMapper;

    private PromotionService promotionService;


    @Override
    public R<Boolean> saveProduct(ProductCombineDTO dto) {

        R<Boolean> validateResult = validate(dto);
        if (validateResult.getCode() != 0) {
            return validateResult;
        }

        //1、保存商品对象
        Product product = new Product();
        BeanUtils.copyProperties(dto, product);
        product.setProductCode(serialService.next(ProductConstant.PRODUCT_CODE_PREFIX, 27));
        Store store = storeService.getDefaultStore();
        if(store==null){
            return R.fail("店铺不存在，不能新增商品");
        }
        product.setStoreId(store.getId());
        baseMapper.insert(product);

        Long productId = product.getId();

//        2、保存商品图片对象
        List<ProductImgSaveDTO> productImgList = dto.getImgs();
        productImgList.forEach(productImgDto -> {
            ProductImg productImg = new ProductImg();
            productImg.setProductId(productId);
            productImg.setImgPath(productImgDto.getImgPath());
            productImgMapper.insert(productImg);
        });

        //3、保存sku规格
        Map<String, ProductAttr> attrMap = new HashMap<String, ProductAttr>();
        Map<String, ProductAttrValue> attrValueMap = new HashMap<String, ProductAttrValue>();
        Map<String, String> attrValueImgMap = new HashMap<String, String>();
        List<ProductAttrCombineSaveDTO> skuAttrList = dto.getSkuAttrList();

        final Boolean needImg = skuAttrList.get(0).getNeedImg();
        final String imgAttrCode = skuAttrList.get(0).getAttrCode();
        if(needImg){
            for(ProductAttrValueSaveDTO attrValueDto:skuAttrList.get(0).getAttrValueList()){
                attrValueImgMap.put(attrValueDto.getAttrValueCode(),attrValueDto.getAttrValueImg());
            }
        }

        skuAttrList.forEach(skuAttrDto -> {
            ProductAttr productAttr = new ProductAttr();
            BeanUtils.copyProperties(skuAttrDto, productAttr);
            productAttr.setProductId(productId);
            productAttrMapper.insert(productAttr);
            attrMap.put(skuAttrDto.getAttrCode(), productAttr);

            Long attrId = productAttr.getId();

            skuAttrDto.getAttrValueList().forEach(attrValueDto -> {
                ProductAttrValue attrValue = new ProductAttrValue();
                BeanUtils.copyProperties(attrValueDto, attrValue);
                attrValue.setAttrId(attrId);
                proudctAttrValueMapper.insert(attrValue);
                attrValueMap.put(attrValueDto.getAttrValueCode(), attrValue);
            });
        });

        //4、保存商品sku
        dto.getSkuList().forEach(skuDto -> {
            ProductSku productSku = new ProductSku();
            BeanUtils.copyProperties(skuDto, productSku);
            productSku.setStoreId(product.getStoreId());
            productSku.setProductId(productId);

            productSku.setSkuNameStr(assemblySkuNameStr(attrValueMap,skuDto.getSkuAttrCodeDTOList()));

            if(needImg){
                skuDto.getSkuAttrCodeDTOList().forEach(skuAttrCodeDTO -> {
                    String attrCode = skuAttrCodeDTO.getAttrCode();
                    if(imgAttrCode.equals(attrCode)){
                        String attrValueCode = skuAttrCodeDTO.getAttrValueCode();
                        String img = attrValueImgMap.get(attrValueCode);
                        productSku.setSkuImg(img);
                    }
                });
            }else{
                //不需要配图，则取商品第一张主图
                productSku.setSkuImg(productImgList.get(0).getImgPath());
            }

            productSkuMapper.insert(productSku);

            skuDto.getSkuAttrCodeDTOList().forEach(skuAttrCodeDTO -> {
                ProductSkuAttr productSkuAttr = new ProductSkuAttr();
                productSkuAttr.setSkuId(productSku.getId());
                productSkuAttr.setAttrId(attrMap.get(skuAttrCodeDTO.getAttrCode()).getId());
                productSkuAttr.setAttrValueId(attrValueMap.get(skuAttrCodeDTO.getAttrValueCode()).getId());
                productSkuAttrMapper.insert(productSkuAttr);
            });
        });

        //4.保存商家设置的折扣信息
        ProductDiscountConfig productDiscount = dto.getProductDiscountConfig();
        productDiscount.setProductId(productId);
        productDiscountConfigMapper.insert(productDiscount);
        //5.保存平台默贡献值信息
        //判断商店类型
        Integer storeType = store.getStoreType();
        ProductContributionConfig productContributionConfig=new ProductContributionConfig();
        productContributionConfig.setProductId(productId);
        if(storeType==0)
        {
            productContributionConfig.setDiscountContribution(24);
            productContributionConfig.setSaleContribution(5);
        }else if(storeType==1){
            productContributionConfig.setDiscountContribution(20);
            productContributionConfig.setSaleContribution(3);
        }else{
            productContributionConfig.setDiscountContribution(16);
            productContributionConfig.setSaleContribution(1);
        }
        productContributionConfigMapper.insert(productContributionConfig);
        //保存平台默认分配比例信息
        ProductDiscContriConfig productDiscContriConfig=new ProductDiscContriConfig();
        productDiscContriConfig.setProductId(productId);
        productDiscContriConfig.setDiscountMember(80);
        productDiscContriConfig.setDiscountMember1(7);
        productDiscContriConfig.setDiscountMember2(3);
        productDiscContriConfig.setDiscountPingtai(10);
        productDiscContriConfig.setContributionMember(80);
        productDiscContriConfig.setContributionMember1(7);
        productDiscContriConfig.setContributionMember2(3);
        productDiscContriConfig.setDiscountTenant(1);
        productDiscContriConfig.setDiscountTenant1(30);
        productDiscContriConfig.setDiscountTenant2(30);
        productDiscContriConfig.setDiscountTenant2(30);
        productDiscContriConfigMapper.insert(productDiscContriConfig);

        //更新商品的价格
        baseMapper.updateProductPrice(product.getId());

        //更新商品的库存
        updateStock(Sets.newHashSet(product.getId()));

        return R.success();
    }


    @Override
    public R<Boolean> updateProduct(ProductCombineDTO dto) {

        //1、更新商品信息
        Product product = new Product();
        BeanUtils.copyProperties(dto, product);
        baseMapper.updateById(product);
        Long productId=product.getId();

        //2、修改商品图片
        List<ProductImgSaveDTO> productImgList = dto.getImgs();
        if (productImgList != null) {
            List<Long> idList = new ArrayList<Long>();
            productImgList.forEach(productImgDto -> {
                if (productImgDto.getId() != null && productImgDto.getId() != 0) {
                    idList.add(productImgDto.getId());
                }
            });
            //将页面删除的图片清除掉
            productImgMapper.delByProudctIdCondition(dto.getId(),idList);

            productImgList.forEach(productImgDto -> {
                if (productImgDto.getId() != null && productImgDto.getId() != 0) {
                    ProductImg productImg = new ProductImg();
                    BeanUtils.copyProperties(productImgDto, productImg);
                    productImgMapper.updateById(productImg);
                } else {//新增
                    ProductImg productImg = new ProductImg();
                    productImg.setProductId(product.getId());
                    productImg.setImgPath(productImgDto.getImgPath());
                    productImgMapper.insert(productImg);
                }
            });
        }
        //更改折扣信息
        ProductDiscountConfig productDiscountConfig = dto.getProductDiscountConfig();
        productDiscountConfigMapper.updateById(productDiscountConfig);

        //3、修改规格 和 规格值
        Map<String, ProductAttr> attrMap = new HashMap<String, ProductAttr>();
        Map<String, ProductAttrValue> attrValueMap = new HashMap<String, ProductAttrValue>();
        Map<String, String> attrValueImgMap = new HashMap<String, String>();
        List<ProductAttrCombineSaveDTO> skuAttrList = dto.getSkuAttrList();

        final Boolean needImg = skuAttrList.get(0).getNeedImg();
        final String imgAttrCode = skuAttrList.get(0).getAttrCode();
        if(needImg){
            for(ProductAttrValueSaveDTO attrValueDto:skuAttrList.get(0).getAttrValueList()){
                attrValueImgMap.put(attrValueDto.getAttrValueCode(),attrValueDto.getAttrValueImg());
            }
        }
        if (skuAttrList != null) {
            List<Long> idList = new ArrayList<Long>();
            skuAttrList.forEach(skuAttrDto -> {
                if(skuAttrDto.getId()!=null && skuAttrDto.getId()!=0&&!skuAttrDto.getAttrName().isEmpty()){
                    idList.add(skuAttrDto.getId());
                }
            });

            //找出那些本次操作删除的规格
            List<Long> deletedAttrIdList = productAttrMapper.selectDeletedAttrIdByCondition(dto.getId(),idList);
            if(deletedAttrIdList.size()>0){


                //删除规格下的 规格值
                productAttrValueMapper.delAttrValueByBatchAttrId(deletedAttrIdList);

                //设置该规格的sku，也要删除掉
                List<Long> skuIdList = productSkuAttrMapper.selectSkuIdListByBatchAttrId(deletedAttrIdList);
                if(skuIdList.size()>0){
                    productSkuMapper.deleteBySkuIdList(skuIdList);
                }

                //删除 sku 和 规格的绑定关系
                productSkuAttrMapper.deleteByAttrIdList(deletedAttrIdList);

                //最后删除这几个规格
                productAttrMapper.delByAttrIdList(deletedAttrIdList);
            }

            skuAttrList.forEach(skuAttrDto -> {
                Long attrId = 0L;
                boolean isAttrAdded = false;
                ProductAttr productAttr = new ProductAttr();
                if (skuAttrDto.getId() != null && skuAttrDto.getId() != 0&&!skuAttrDto.getAttrName().isEmpty()) {
                    attrId = skuAttrDto.getId();
                    //更新
                    BeanUtils.copyProperties(skuAttrDto, productAttr);
                    productAttrMapper.updateById(productAttr);
                } else {//新增
                    isAttrAdded = true;
                    BeanUtils.copyProperties(skuAttrDto, productAttr);
                    productAttr.setProductId(dto.getId());
                    productAttrMapper.insert(productAttr);
                    attrId = productAttr.getId();
                }
                attrMap.put(skuAttrDto.getAttrCode(), productAttr);

                //处理修改的规格，规格值被删除的情况
                if(!isAttrAdded){

                    List<Long> notDeletedAttrValueIdList = new ArrayList<Long>();
                    skuAttrDto.getAttrValueList().forEach(attrValueDto -> {
                        if(attrValueDto.getId()!=null && attrValueDto.getId()!=0){
                            notDeletedAttrValueIdList.add(attrValueDto.getId());
                        }
                    });

                    //逻辑删除 本次操作删除的规格值
                    productAttrValueMapper.delByAttrIdAndNotInIdList(attrId,notDeletedAttrValueIdList);

                    //逻辑删除 删除的规格值 对应的sku
                    if(notDeletedAttrValueIdList.size()>0){
                        //这里查出来的是本次保留的规格值对应的sku，假设原来是颜色：红色,蓝色,黄色 本次删除黄色，则有红色和蓝色的sku会保留下来
                        List<Long> skuIdList = productSkuAttrMapper.selectSkuIdListByBatchAttrValueId(notDeletedAttrValueIdList);

                        //查找要删除的其它sku
                        List<Long> shouldDeletedSkuIdList = productSkuMapper.selectByProductIdAndSkuIdNotInList(dto.getId(),skuIdList);

                        if(shouldDeletedSkuIdList.size()>0){
                            productSkuMapper.deleteBySkuIdList(shouldDeletedSkuIdList);

                            //删除其它sku对应的sku规格信息
                            productSkuAttrMapper.delSkuAttrByBatchSkuId(shouldDeletedSkuIdList);
                        }
                    }else{
                        List<Long> shouldDeletedSkuIdList = productSkuMapper.selectByProductIdAndSkuIdNotInList(dto.getId(),null);
                        if(shouldDeletedSkuIdList.size()>0){
                            productSkuMapper.deleteBySkuIdList(shouldDeletedSkuIdList);

                            //删除其它sku对应的sku规格信息
                            productSkuAttrMapper.delSkuAttrByBatchSkuId(shouldDeletedSkuIdList);
                        }
                    }
                }

                final Long finalAttrId = attrId;
                skuAttrDto.getAttrValueList().forEach(attrValueDto -> {
                    Long attrValueId = attrValueDto.getId();
                    ProductAttrValue attrValue = new ProductAttrValue();
                    if (attrValueId != null && attrValueId != 0) {
                        BeanUtils.copyProperties(attrValueDto, attrValue);
                        productAttrValueMapper.updateById(attrValue);
                    } else {//新增
                        BeanUtils.copyProperties(attrValueDto, attrValue);
                        attrValue.setAttrId(finalAttrId);
                        proudctAttrValueMapper.insert(attrValue);
                    }
                    attrValueMap.put(attrValueDto.getAttrValueCode(), attrValue);
                });
            });
        }

        //修改 商品sku
//        if (dto.getSkuList() != null) {
//
//            //处理本次删除的sku
//            List<Long> skuIdList = new ArrayList<Long>();
//            dto.getSkuList().forEach(skuDto -> {
//                if(skuDto.getId()!=null && skuDto.getId()!=0){
//                    skuIdList.add(skuDto.getId());
//                }
//            });
//
//            List<Long> shouldDeletedSkuIdList = productSkuMapper.selectByProductIdAndSkuIdNotInList(dto.getId(),skuIdList);
//
//            if(shouldDeletedSkuIdList.size()>0){
//                productSkuMapper.deleteBySkuIdList(shouldDeletedSkuIdList);
//                productSkuAttrMapper.delSkuAttrByBatchSkuId(shouldDeletedSkuIdList);
//            }
//
//            dto.getSkuList().forEach(skuDto -> {
//                Long skuId = skuDto.getId();
//                ProductSku productSku = new ProductSku();
//                BeanUtils.copyProperties(skuDto, productSku);
//                productSku.setProductId(product.getId());
//                productSku.setStoreId(product.getStoreId());
//                productSku.setSkuNameStr(assemblySkuNameStr(attrValueMap,skuDto.getSkuAttrCodeDTOList()));
//                if(needImg){
//                    skuDto.getSkuAttrCodeDTOList().forEach(skuAttrCodeDTO -> {
//                        String attrCode = skuAttrCodeDTO.getAttrCode();
//                        if(imgAttrCode.equals(attrCode)){
//                            String attrValueCode = skuAttrCodeDTO.getAttrValueCode();
//                            String img = attrValueImgMap.get(attrValueCode);
//                            productSku.setSkuImg(img);
//                        }
//                    });
//                }else{
//                    productSku.setSkuImg(null);
//                }
//
//
//                if (skuId != null && skuId != 0) {
//                    //更新
//                    productSkuMapper.updateById(productSku);
//                } else {//新增
//                    productSkuMapper.insert(productSku);
//                    skuDto.getSkuAttrCodeDTOList().forEach(skuAttrCodeDTO -> {
//                        ProductSkuAttr productSkuAttr = new ProductSkuAttr();
//                        productSkuAttr.setSkuId(productSku.getId());
//                        productSkuAttr.setAttrId(attrMap.get(skuAttrCodeDTO.getAttrCode()).getId());
//                        productSkuAttr.setAttrValueId(attrValueMap.get(skuAttrCodeDTO.getAttrValueCode()).getId());
//                        productSkuAttrMapper.insert(productSkuAttr);
//                    });
//                }
//            });
//        }




        //4、更新商品sku
        //删除旧的sku
        List<Long> skuIdList = new ArrayList<Long>();
        dto.getSkuList().forEach(skuDto -> {
            skuIdList.add(skuDto.getId());
        });
        productSkuMapper.deleteBySkuIdList(skuIdList);
        productSkuAttrMapper.delSkuAttrByBatchSkuId(skuIdList);


        dto.getSkuList().forEach(skuDto -> {
            ProductSku productSku = new ProductSku();
            BeanUtils.copyProperties(skuDto, productSku);
            productSku.setStoreId(product.getStoreId());
            productSku.setProductId(productId);

            productSku.setSkuNameStr(assemblySkuNameStr(attrValueMap,skuDto.getSkuAttrCodeDTOList()));

            if(needImg){
                skuDto.getSkuAttrCodeDTOList().forEach(skuAttrCodeDTO -> {
                    String attrCode = skuAttrCodeDTO.getAttrCode();
                    if(imgAttrCode.equals(attrCode)){
                        String attrValueCode = skuAttrCodeDTO.getAttrValueCode();
                        String img = attrValueImgMap.get(attrValueCode);
                        productSku.setSkuImg(img);
                    }
                });
            }else{
                //不需要配图，则取商品第一张主图
                productSku.setSkuImg(productImgList.get(0).getImgPath());
            }

            productSku.setId(null);
            productSkuMapper.insert(productSku);

            skuDto.getSkuAttrCodeDTOList().forEach(skuAttrCodeDTO -> {
                ProductSkuAttr productSkuAttr = new ProductSkuAttr();
                productSkuAttr.setSkuId(productSku.getId());
                productSkuAttr.setAttrId(attrMap.get(skuAttrCodeDTO.getAttrCode()).getId());
                productSkuAttr.setAttrValueId(attrValueMap.get(skuAttrCodeDTO.getAttrValueCode()).getId());
                productSkuAttr.setId(null);
                productSkuAttrMapper.insert(productSkuAttr);
            });
        });

        //更新商品的价格
        baseMapper.updateProductPrice(product.getId());

        //更新商品的缓存
        queryProductAndSetCache(product.getId());

        //设置商品库存
        updateStock(Sets.newHashSet(product.getId()));

        return R.success();
    }

    @Override
    public R<Boolean> onlineProduct(ProductUpdateDTO dto) {
        baseMapper.onlineProduct(dto.getId());
        return R.success();
    }

    @Override
    public R<Boolean> offlineProduct(ProductUpdateDTO dto) {
        baseMapper.offlineProduct(dto.getId());
        return R.success();
    }

    @Override
    public R<Boolean> delProduct(IdQueryDTO dto) {
        baseMapper.delProductById(dto.getId());
        return R.success();
    }

    @Override
    public R<ProductCombineDTO> queryById(Long id) {
        Product product = baseMapper.selectById(id);
        if (product == null) {
            return R.fail("商品不存在");
        }
        ProductCombineDTO dto = new ProductCombineDTO();
        BeanUtils.copyProperties(product, dto);
        dto.setId(id);

        //设置 商品图片
        List<ProductImgSaveDTO> imgDtoList = new ArrayList<ProductImgSaveDTO>();
        List<ProductImg> imgList = productImgMapper.selectByProductId(id);
        for (ProductImg img : imgList) {
            ProductImgSaveDTO imgDto = new ProductImgSaveDTO();
            BeanUtils.copyProperties(img, imgDto);
            imgDto.setId(img.getId());
            imgDtoList.add(imgDto);
        }
        dto.setImgs(imgDtoList);

        //设置商品的规格 和 规格值
        List<ProductAttrCombineSaveDTO> attrCombineList = new ArrayList<ProductAttrCombineSaveDTO>();
        List<ProductAttr> attrList = productAttrMapper.selectByProductId(id);
        for (ProductAttr attr : attrList) {
            ProductAttrCombineSaveDTO attrDto = new ProductAttrCombineSaveDTO();
            BeanUtils.copyProperties(attr, attrDto);
            attrDto.setId(attr.getId());

            List<ProductAttrValueSaveDTO> attrValueDtoList = new ArrayList<ProductAttrValueSaveDTO>();
            List<ProductAttrValue> attrValueList = productAttrValueMapper.selectByAttrId(attr.getId());
            for (ProductAttrValue attrValue : attrValueList) {
                ProductAttrValueSaveDTO attrValueDto = new ProductAttrValueSaveDTO();
                BeanUtils.copyProperties(attrValue, attrValueDto);
                attrValueDto.setId(attrValue.getId());
                attrValueDtoList.add(attrValueDto);
            }
            attrDto.setAttrValueList(attrValueDtoList);
            attrCombineList.add(attrDto);
        }

        dto.setSkuAttrList(attrCombineList);

        //设置商品的sku
        List<ProductSkuSaveDTO> skuDtoList = new ArrayList<ProductSkuSaveDTO>();
        List<ProductSku> skuList = productSkuMapper.selectByProductId(id);
        for (ProductSku sku : skuList) {
            ProductSkuSaveDTO skuDto = new ProductSkuSaveDTO();
            BeanUtils.copyProperties(sku, skuDto);
            skuDto.setId(sku.getId());

            List<ProductSkuAttr> skuAttrList = productSkuAttrMapper.selectBySkuId(sku.getId());

            skuDto.setSkuAttrList(skuAttrList);
            skuDtoList.add(skuDto);
        }

        dto.setSkuList(skuDtoList);

        return R.success(dto);
    }

    @Override
    public R<ProductVO> queryProductDetail(Long id) {

        ProductVO vo = redisRepository.get(ProductConstant.PRODUCT_VO_PREFIX + id);

        if (vo != null) {
            return R.success(vo);
        }

        return queryProductAndSetCache(id);
    }

    @Override
    public R<ProductSkuVO> queryProductSku(QueryProductSkuDTO queryProductSkuDTO) {
        if(queryProductSkuDTO==null || queryProductSkuDTO.getProductId()==null
                || queryProductSkuDTO.getSkuValueIdList()==null || queryProductSkuDTO.getSkuValueIdList().size()==0){
            return R.fail("参数错误");
        }

        List<Long> skuValueIdList = queryProductSkuDTO.getSkuValueIdList();
        List<ProductSku> productSkuList = productSkuMapper.selectList(Wraps.<ProductSku>lbQ().eq(ProductSku::getProductId,queryProductSkuDTO.getProductId()).eq(ProductSku::getIsDelete,0));
        for(ProductSku sku:productSkuList){
            List<ProductSkuAttr> skuAttrList = productSkuAttrMapper.selectBySkuId(sku.getId());
            if(skuAttrList.size()!=skuValueIdList.size()){
                continue;
            }
            int matchCount = 0;
            for(ProductSkuAttr skuAttr:skuAttrList){
                for(Long skuValueId:skuValueIdList){
                    if(skuValueId.equals(skuAttr.getAttrValueId())){
                        matchCount++;
                    }
                }
            }
            if(matchCount==skuValueIdList.size()){
                return R.success(dozerUtils.map(sku,ProductSkuVO.class));
            }
        }

        return R.success(null);
    }

    @Override
    public R<ProductPageVO> findAdminStoreProductList(AdminStoreProductDTO adminStoreProductDTO) {
        if(adminStoreProductDTO==null){
            return R.fail("参数错误");
        }

        if(adminStoreProductDTO.getPageIndex()==null){
            adminStoreProductDTO.setPageIndex(1);
        }
        if(adminStoreProductDTO.getPageSize()==null){
            adminStoreProductDTO.setPageSize(10);
        }

        StringBuilder orderByBuilder = new StringBuilder("");

        //按新品排序
        if(adminStoreProductDTO.getNewItem()!=null && adminStoreProductDTO.getNewItem()==1){
            orderByBuilder.append(" create_time desc,");
        }

        //按价格排序
        if(adminStoreProductDTO.getPrice()!=null){
            if(adminStoreProductDTO.getPrice()==1){
                orderByBuilder.append(" price desc,");
            }else{
                orderByBuilder.append(" price asc,");
            }
        }

        //按销量排序
        if(adminStoreProductDTO.getSellCount()!=null){
            if(adminStoreProductDTO.getSellCount()==1){
                orderByBuilder.append(" sell_count desc,");
            }else{
                orderByBuilder.append(" sell_count asc,");
            }
        }

        if(orderByBuilder.length()!=0){
            orderByBuilder.deleteCharAt(orderByBuilder.length()-1);
            adminStoreProductDTO.setOrderBy(orderByBuilder.toString());
        }

        adminStoreProductDTO.setStart((adminStoreProductDTO.getPageIndex()-1)*adminStoreProductDTO.getPageSize());

        Integer total = baseMapper.findAdminStoreProductListCount(adminStoreProductDTO);
        List<Product> productList = baseMapper.findAdminStoreProductList(adminStoreProductDTO);

        List<ProductVO> productVOList = dozerUtils.mapList(productList,ProductVO.class);
        for(ProductVO productVO:productVOList){
            List<ProductImg> productImgs = productImgMapper.selectByProductId(productVO.getId());
            List<String> imgs = new ArrayList<String>();
            for(ProductImg productImg:productImgs){
                imgs.add(productImg.getImgPath());
            }
            productVO.setImgs(imgs);

            //查询付款人数
            Integer buyCount = baseMapper.getProductBuyCount(productVO.getId());
            productVO.setBuyCount(buyCount);
        }

        ProductPageVO pageVO = new ProductPageVO();
        pageVO.setTotal(total);
        pageVO.setProductVOList(productVOList);

        return R.success(pageVO);
    }

    @Override
    public R<ProductGroupListVO> findProductGroupList(ProductGroupQueryDTO productGroupQueryDTO) {
        ProductGroupListVO listVO = new ProductGroupListVO();
        int start = (productGroupQueryDTO.getPageIndex()-1)*productGroupQueryDTO.getPageSize();
        Integer total = baseMapper.findProductGroupCount();
        List<Long> groupIdList = baseMapper.findProductGroupList(start,productGroupQueryDTO.getPageSize());
        List<ProductGroupVO> groupVOList = new ArrayList<ProductGroupVO>();
        for(Long groupId:groupIdList){
            ProductGroup productGroup = productGroupMapper.selectById(groupId);
            if(productGroup!=null){
                ProductGroupVO groupVO = new ProductGroupVO();
                groupVO.setId(groupId);
                groupVO.setGroupName(productGroup.getGroupName());
                groupVOList.add(groupVO);
            }
        }

        listVO.setTotal(total);
        listVO.setProductGroupVOList(groupVOList);
        return R.success(listVO);
    }

    @Override
    public R<ProductPageVO> findProductListByGroupId(ProductListByGroupIdQueryDTO productListByGroupIdQueryDTO) {
        if(productListByGroupIdQueryDTO==null){
            return R.fail("参数错误");
        }

        productListByGroupIdQueryDTO.setStart((productListByGroupIdQueryDTO.getPageIndex()-1)*productListByGroupIdQueryDTO.getPageSize());
        Integer total = baseMapper.selectCount(Wraps.<Product>lbQ().eq(Product::getGroupId,productListByGroupIdQueryDTO.getGroupId()).eq(Product::getIsDelete,0));
        List<Product> productList = baseMapper.findProductListByGroupId(productListByGroupIdQueryDTO);

        List<ProductVO> productVOList = dozerUtils.mapList(productList,ProductVO.class);
        for(ProductVO productVO:productVOList){
            List<ProductImg> productImgs = productImgMapper.selectByProductId(productVO.getId());
            List<String> imgs = new ArrayList<String>();
            for(ProductImg productImg:productImgs){
                imgs.add(productImg.getImgPath());
            }
            productVO.setImgs(imgs);

            //查询付款人数
            Integer buyCount = baseMapper.getProductBuyCount(productVO.getId());
            productVO.setBuyCount(buyCount);
        }

        ProductPageVO pageVO = new ProductPageVO();
        pageVO.setTotal(total);
        pageVO.setProductVOList(productVOList);

        return R.success(pageVO);
    }

    @Override
    public R<AdminProductPageVO> findPlatformAdminProductList(AdminProductDTO dto) {
        if(dto==null){
            return R.fail("参数错误");
        }

        //如果关键词是空格字符，设置成空字符串
        dto.setKeyword(StringUtils.trimToEmpty(dto.getKeyword()));

        dto.setStart((dto.getPageIndex()-1)*dto.getPageSize());
        List<Product> productList = baseMapper.selectPlatformAdminProductListByCondition(dto);
        List<AdminProductVO> adminProductVOList = dozerUtils.mapList(productList,AdminProductVO.class);

        adminProductVOList.forEach(productVO->{
            Long id = productVO.getId();
            List<ProductImg> productImgList = productImgMapper.selectByProductId(id);
            if(productImgList.size()>0){
                productVO.setProductImg(productImgList.get(0).getImgPath());
            }
        });

        Integer total = baseMapper.selectPlatformCountAdminProductListByCondition(dto);

        AdminProductPageVO pageVO = new AdminProductPageVO();
        pageVO.setAdminProductVOList(adminProductVOList);
        pageVO.setTotal(total);

        return R.success(pageVO);
    }

    @Override
    public R<CategoryProductCombineVO> findProductListByCategoryOrProductIdList(CategoryProductCombineDTO combineDTO) {

        CategoryProductCombineVO combineVO = new CategoryProductCombineVO();

        if(combineDTO.getCategoryIdList()!=null){
            List<Long> category3IdList = new ArrayList<Long>();
            for(Long categoryId:combineDTO.getCategoryIdList()){
                ProductCategory category = productCategoryMapper.selectById(categoryId);
                if(category.getDepth()==3){
                    category3IdList.add(categoryId);
                }else if(category.getDepth()==2){
                    List<Long> productCategoryIdList = productCategoryMapper.selectCategoryIdByParentId(category.getId());
                    category3IdList.addAll(productCategoryIdList);
                }else if(category.getDepth()==1){
                    List<Long> productCategory2IdList = productCategoryMapper.selectCategoryIdByParentId(category.getId());
                    for(Long category2Id:productCategory2IdList){
                        List<Long> productCategory3IdList = productCategoryMapper.selectCategoryIdByParentId(category2Id);
                        category3IdList.addAll(productCategory3IdList);
                    }
                }
            }
            if(category3IdList.size()>0){
                List<Product> productList = baseMapper.selectByCategoryIdListIgnoreTenant(category3IdList);

                List<SimpleProductVO> productVOList = dozerUtils.mapList(productList,SimpleProductVO.class);
                for(SimpleProductVO productVO:productVOList){
                    List<ProductImg> productImgs = productImgMapper.selectByProductId(productVO.getId());
                    List<String> imgs = new ArrayList<String>();
                    for(ProductImg productImg:productImgs){
                        imgs.add(productImg.getImgPath());
                    }
                    productVO.setImgs(imgs);

                    //查询付款人数
                    Integer buyCount = baseMapper.getProductBuyCount(productVO.getId());
                    productVO.setBuyCount(buyCount);

                    Store store = storeService.findByTenantCode(productVO.getTenantCode());
                    if (store != null) {
                        productVO.setTenantCode(storeService.findTenantCodeByStoreId(productVO.getStoreId()));
                        productVO.setStoreName(store.getStoreName());
                        productVO.setStoreLogo(store.getLogo());
                    }
                }
                combineVO.setCategoryList(productVOList);
            }
        }

        if(combineDTO.getProductIdList()!=null && combineDTO.getProductIdList().size()>0){
            List<Product> productList = baseMapper.selectListIgnoreTenant(combineDTO.getProductIdList());

            List<SimpleProductVO> productVOList = dozerUtils.mapList(productList,SimpleProductVO.class);
            for(SimpleProductVO productVO:productVOList){
                List<ProductImg> productImgs = productImgMapper.selectByProductId(productVO.getId());
                List<String> imgs = new ArrayList<String>();
                for(ProductImg productImg:productImgs){
                    imgs.add(productImg.getImgPath());
                }
                productVO.setImgs(imgs);

                //查询付款人数
                Integer buyCount = baseMapper.getProductBuyCount(productVO.getId());
                productVO.setBuyCount(buyCount);

                Store store = storeService.findByTenantCode(productVO.getTenantCode());
                if (store != null) {
                    productVO.setTenantCode(storeService.findTenantCodeByStoreId(productVO.getStoreId()));
                    productVO.setStoreName(store.getStoreName());
                    productVO.setStoreLogo(store.getLogo());
                }
            }
            combineVO.setGoodsList(productVOList);
        }

        return R.success(combineVO);
    }

    @Override
    public TenantProduct getByIdWithTenant(Long id) {
        return baseMapper.getByIdWithTenant(id);
    }

    @Override
    public R<AdminProductPageVO> findAdminProductList(AdminProductDTO dto) {

        if(dto==null){
            return R.fail("参数错误");
        }

        //keywordType==0，也要设置成null，
        if(dto.getKeywordType()!=null && dto.getKeywordType()==0){
            dto.setKeywordType(null);
        }
        //如果关键词是空格字符，设置成空字符串
        dto.setKeyword(StringUtils.trimToEmpty(dto.getKeyword()));

        dto.setStart((dto.getPageIndex()-1)*dto.getPageSize());
        List<Product> productList = baseMapper.selectAdminProductListByCondition(dto);
        List<AdminProductVO> adminProductVOList = dozerUtils.mapList(productList,AdminProductVO.class);

        adminProductVOList.forEach(productVO->{
            Long id = productVO.getId();
            List<ProductImg> productImgList = productImgMapper.selectByProductId(id);
            if(productImgList.size()>0){
                productVO.setProductImg(productImgList.get(0).getImgPath());
            }
        });

        Integer total = baseMapper.selectCountAdminProductListByCondition(dto);

        AdminProductPageVO pageVO = new AdminProductPageVO();
        pageVO.setAdminProductVOList(adminProductVOList);
        pageVO.setTotal(total);

        return R.success(pageVO);
    }

    @Override
    public R<List<ShoppingCartSkuVO>> assembleShoppingCartSku(List<Long> skuIdList) {

        List<ShoppingCartSkuVO> shoppingCartSkuVOList = new ArrayList<>();

        try{
            TreeMap<Long, List<ShoppingCartSkuItemVO>> map = new TreeMap<Long, List<ShoppingCartSkuItemVO>>();
            for(Long skuId:skuIdList){
                ProductSku sku = productSkuMapper.selectById(skuId);
                Product product = baseMapper.selectById(sku.getProductId());
                Long storeId = product.getStoreId();
                if(map.get(storeId)==null){
                    map.put(storeId,new ArrayList<>());
                }
                ShoppingCartSkuItemVO skuItemVO = dozerUtils.map(sku,ShoppingCartSkuItemVO.class);
                skuItemVO.setProductName(product.getProductName());
                skuItemVO.setSkuName(sku.getSkuNameStr());
                map.get(storeId).add(skuItemVO);
            }


            for(Map.Entry<Long,List<ShoppingCartSkuItemVO>> entry:map.entrySet()){
                Long storeId = entry.getKey();
                Store store = storeService.getById(storeId);
                ShoppingCartSkuVO cartSkuVO = new ShoppingCartSkuVO();
                cartSkuVO.setStoreId(storeId);
                cartSkuVO.setStoreName(store.getStoreName());
                cartSkuVO.setShoppingCartSkuItemList(entry.getValue());
                shoppingCartSkuVOList.add(cartSkuVO);
            }
        }catch(Exception e){
            log.error(e.getMessage(),e);
        }

        return R.success(shoppingCartSkuVOList);
    }

    @Override
    public R<ProductPageVO> searchProduct(SearchProductDTO searchProductDTO) {
        try{
            if(searchProductDTO==null){
                return R.success(new ProductPageVO());
            }

            //记录搜索历史
            recordSearchHistory(searchProductDTO);

            String keyword = searchProductDTO.getKeyword();
            Integer pageIndex = searchProductDTO.getPageIndex();
            Integer pageSize = searchProductDTO.getPageSize();
            Long category3Id = searchProductDTO.getCategory3Id();
            Long promotionId = searchProductDTO.getPromotionId();
            if(pageIndex==null || pageIndex<1){
                pageIndex = 1;
            }
            if(pageSize == null || pageSize<=0 || pageSize>50){
                pageSize = 10;
            }
            int start = (pageIndex-1) * pageSize;
            Integer count = baseMapper.searchItemCount(keyword,category3Id,promotionId);
            List<Product> productList = baseMapper.searchItem(keyword,category3Id,promotionId,start,pageSize);
            List<ProductVO> productVOList = dozerUtils.mapList(productList,ProductVO.class);
            Map<Long,StoreDTO> storeMap = new HashMap<Long,StoreDTO>();
            for(ProductVO productVO:productVOList){
                List<ProductImg> productImgList = productImgMapper.selectList(Wraps.<ProductImg>lbQ().eq(ProductImg::getProductId,productVO.getId())
                        .eq(ProductImg::getIsDelete,0).orderByAsc(ProductImg::getSortOrder));
                List<String> imgs = new ArrayList<String>();
                if(productImgList!=null){
                    for(ProductImg productImg:productImgList){
                        imgs.add(productImg.getImgPath());
                    }
                }
                productVO.setImgs(imgs);
                StoreDTO storeDTO = storeMap.get(productVO.getStoreId());
                if(storeDTO!=null){
                    productVO.setStoreDTO(storeDTO);
                }else{
                    Store store = storeService.getById(productVO.getStoreId());
                    storeMap.put(productVO.getStoreId(),dozerUtils.map(store,StoreDTO.class));
                    productVO.setStoreDTO(storeMap.get(productVO.getStoreId()));
                }
            }

            for(ProductVO productVO:productVOList){
                Long productId = productVO.getId();
                productVO.setPromotionShowVOList(promotionService.findPromotionListByProductId(productId));
            }

            //这里查询的是所有符合条件的商品，所参加的活动，不是仅限于当前分页的商品
            List<PromotionShowVO> promotionShowVOList = baseMapper.searchItemPromotion(keyword,category3Id);

            ProductPageVO productPageVO = new ProductPageVO(count,productVOList,promotionShowVOList);
            return R.success(productPageVO);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return R.fail("查询失败");
        }
    }

    @Override
    public void updateStock(Set<Long> productIdSet) {
        if(productIdSet==null || productIdSet.size()==0){
            return;
        }
        for(Long productId:productIdSet){
            Integer stock = productSkuMapper.selectSumStock(productId);
            baseMapper.updateStock(productId,stock);
        }
    }

    /** 插入搜索历史
     * @author JustArgo 2020/5/31 12:43
     * @return
     */
    private void recordSearchHistory(SearchProductDTO searchProductDTO) {
        if(searchProductDTO!=null && searchProductDTO.getSource()!=null
                && searchProductDTO.getSource()==1 && StringUtils.isNotBlank(searchProductDTO.getKeyword())
                && searchProductDTO.getMid()!=null){
            SearchHistory searchHistory = new SearchHistory();
            searchHistory.setMid(searchProductDTO.getMid());
            searchHistory.setKeyword(StringUtils.trim(searchProductDTO.getKeyword()));
            searchHistoryMapper.insert(searchHistory);
        }
    }

    /** 查询最近的N条搜索历史记录
     * @author JustArgo 2020/5/31 12:46
     * @return
     */
    @Override
    public R<List<SearchHistory>> findLastNSearchHistory(Long mid, int nCount){
        if(mid!=null && nCount>0){
            List<SearchHistory> historyList = searchHistoryMapper.findLastNSearchHistory(mid,nCount);
            return R.success(historyList);
        }
        return R.success(Collections.emptyList());
    }

    /** 删除搜索历史
     * @author JustArgo 2020/5/31 13:01
     * @return
     */
    @Override
    public R<Boolean> delSearchHistory(Long mid, String keyword){
        if(mid!=null && StringUtils.isNotBlank(keyword)){
            searchHistoryMapper.delSearchHistory(mid, keyword);
            return R.success(true);
        }
        return R.success(false);
    }

    @Override
    public void updateSellCount(Map<Long, Integer> productSellCountMap) {
        if(productSellCountMap!=null && productSellCountMap.size()!=0){
            for(Map.Entry<Long,Integer> entry:productSellCountMap.entrySet()){
                Long productId = entry.getKey();
                Integer sellCount = entry.getValue();
                if(productId!=null && sellCount!=null){
                    baseMapper.updateSellCount(productId,sellCount);
                }
            }
        }
    }

    @Override
    public Long getStoreProductSellCount(Long storeId) {
        return baseMapper.selectStoreSumSellCount(storeId);
    }

    @Override
    public List<AdminProductVO> findAdminProductListByIds(List<Long> productIds) {
        if(productIds==null || productIds.size()==0){
            return Collections.emptyList();
        }

        List<Product> productList = baseMapper.selectListIgnoreTenant(productIds);
        List<AdminProductVO> adminProductVOList = dozerUtils.mapList(productList,AdminProductVO.class);

        adminProductVOList.forEach(productVO->{
            Long id = productVO.getId();
            List<ProductImg> productImgList = productImgMapper.selectByProductId(id);
            if(productImgList.size()>0){
                productVO.setProductImg(productImgList.get(0).getImgPath());
            }
        });

        return adminProductVOList;
    }

    @Override
    public Product getByIdIgnoreTenant(Long productId) {
        if(productId == null){
            return null;
        }
        List<Product> productList = baseMapper.selectListIgnoreTenant(Arrays.asList(productId));
        if(productList != null && productList.size()>0){
            return productList.get(0);
        }
        return null;
    }


    private R<Boolean> validate(ProductCombineDTO dto) {
        R<Boolean> result = R.fail("参数错误");
        if (dto == null) {
            result.setMsg("商品不能为空");
            return result;
        }
        /*if (dto.getShortName() != null && dto.getShortName().length() > 50) {
            result.setMsg("商品短名不能超过10个字符");
            return result;
        }*/
        if (dto.getCategoryId() == null) {
            result.setMsg("类目不能为空");
            return result;
        }
        if (dto.getImgs() == null || dto.getImgs().size() == 0) {
            result.setMsg("图片不能为空");
            return result;
        }
        if (dto.getImgs().size() > 20) {
            result.setMsg("图片不能超过20张");
            return result;
        }
        if (dto.getSkuList() == null || dto.getSkuList().size() == 0) {
            result.setMsg("至少要设置一个sku");
            return result;
        }
        if (dto.getAttrStyle() == null) {
            result.setMsg("必须选择单款式或者多款式");
            return result;
        }
        //多款式判断商品规格
        if (dto.getAttrStyle() == 1) {
            if (dto.getSkuAttrList() == null || dto.getSkuAttrList().size() == 0) {
                result.setMsg("多款式商品必须至少有一个规格");
                return result;
            }
        }
        return R.success();
    }

    private R<ProductVO> queryProductAndSetCache(Long id) {
        Product product = baseMapper.selectById(id);
        if (product == null) {
            return R.fail("商品不存在");
        }
        ProductVO vo = new ProductVO();
        BeanUtils.copyProperties(product, vo);
        vo.setId(id);

        //设置 商品图片
        List<ProductImgSaveDTO> imgDtoList = new ArrayList<ProductImgSaveDTO>();
        List<ProductImg> imgList = productImgMapper.selectByProductId(id);
        List<String> imgs = new ArrayList<String>();
        for (ProductImg img : imgList) {
            imgs.add(img.getImgPath());
        }
        vo.setImgs(imgs);

        //设置商品的规格 和 规格值
        List<ProductAttrCombineSaveDTO> attrCombineList = new ArrayList<ProductAttrCombineSaveDTO>();
        List<ProductAttr> attrList = productAttrMapper.selectByProductId(id);

        for (ProductAttr attr : attrList) {
            ProductAttrCombineSaveDTO attrDto = new ProductAttrCombineSaveDTO();
            BeanUtils.copyProperties(attr, attrDto);
            attrDto.setId(attr.getId());

            List<ProductAttrValueSaveDTO> attrValueDtoList = new ArrayList<ProductAttrValueSaveDTO>();
            List<ProductAttrValue> attrValueList = productAttrValueMapper.selectByAttrId(attr.getId());
            for (ProductAttrValue attrValue : attrValueList) {
                ProductAttrValueSaveDTO attrValueDto = new ProductAttrValueSaveDTO();
                BeanUtils.copyProperties(attrValue, attrValueDto);
                attrValueDto.setId(attrValue.getId());
                attrValueDtoList.add(attrValueDto);
            }
            attrDto.setAttrValueList(attrValueDtoList);
            attrCombineList.add(attrDto);
        }

        vo.setAttrList(attrCombineList);

        //设置sku
        List<ProductSku> productSkuList = productSkuMapper.selectByProductId(id);
        if(productSkuList!=null){
            List<ProductSkuVO> skuVOList = new ArrayList<ProductSkuVO>();
            for(ProductSku sku:productSkuList){
                ProductSkuVO skuVO = dozerUtils.map(sku,ProductSkuVO.class);
                List<ProductSkuAttr> skuAttrList = productSkuAttrMapper.selectBySkuId(skuVO.getId());
                skuVO.setSkuAttrList(skuAttrList);
                skuVOList.add(skuVO);
            }
            vo.setProductSkuVOList(skuVOList);
        }

        int productCount = this.count(Wraps.<Product>lbQ().eq(Product::getStoreId,product.getStoreId()).eq(Product::getIsDelete,0));
        Integer soldNum = baseMapper.selectStoreSellCount(product.getStoreId());

        Store store = storeService.getById(product.getStoreId());

        StoreDTO storeDTO = new StoreDTO();
        storeDTO.setId(product.getStoreId());
        storeDTO.setStoreName(store.getStoreName());
        storeDTO.setProductCount(productCount);
        storeDTO.setSoldNum(soldNum==null?0:soldNum);
        storeDTO.setLogo(store.getLogo());
        vo.setStoreDTO(storeDTO);

        //设置店铺的发货地址
        vo.setShipAddress(store.getShipAddress());

        if(vo.getPostFee()==null){
            vo.setPostFee(0L);
        }

        //查询付款人数
        Integer buyCount = baseMapper.getProductBuyCount(vo.getId());
        vo.setBuyCount(buyCount);

        //设置参加的活动列表
        vo.setPromotionShowVOList(promotionService.findPromotionListByProductId(vo.getId()));

        redisRepository.setExpire(ProductConstant.PRODUCT_VO_PREFIX + vo.getId(), vo, ProductConstant.PRODUCT_VO_CACHE_TIME);

        return R.success(vo);
    }

    /** 组装sku的最新名称
     * @author JustArgo 2020/5/30 17:43
     * @return
     */
    private String assemblySkuNameStr(Map<String, ProductAttrValue> attrValueMap, List<SkuAttrCodeDTO> skuAttrCodeDTOList){
        StringBuilder builder = new StringBuilder();
        for(SkuAttrCodeDTO attrCodeDTO:skuAttrCodeDTOList){
            ProductAttrValue attrValue = attrValueMap.get(attrCodeDTO.getAttrValueCode());
            builder.append(attrValue.getAttrValue()).append(" ");
        }
        builder.deleteCharAt(builder.length()-1);
        return builder.toString();
    }


    @Override
    public R<Boolean> updateProductDiscContri(ProductDiscContriConfigUpdateDTO dto) {
        ProductDiscContriConfig productDiscContriConfig=new ProductDiscContriConfig();
        BeanUtils.copyProperties(dto, productDiscContriConfig);
        productDiscContriConfigMapper.updateById(productDiscContriConfig);
        return R.success();
    }

    @Override
    public R<Boolean> updateProductContribution(ProductContributionConfigUpdateDTO dto) {
        ProductContributionConfig productContributionConfig=new ProductContributionConfig();
        BeanUtils.copyProperties(dto, productContributionConfig);
        productContributionConfigMapper.updateById(productContributionConfig);
        return R.success();
    }
}
